import { getRegular, getRegularDetail, getRegularHistory, getRegularHistoryPerson } from '@/services/onboardAndLeave/regularization';

export default {
    namespace: 'regularization',
    state: {
        data: [],
        historyData: [],
        pagination: {
            current: 1,
            pageSize: 10,
            total: 0
        }
    },
    effects: {
        // 转正列表
        *fetchList({ payload }, { call, put }) {
            const result = yield call(getRegular, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        data: result.data.rows,
                        pagination: {
                            pageSize: result.data.pageSize,
                            current: result.data.pageNo,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        // 获取转正详情
        *getDetail({ payload, callback }, { call, put }) {
            const response = yield call(getRegularDetail, payload);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        userData: response.data
                    }
                });
            }
            callback(response);
        },
        // 获取转正历史记录-企业管理
        *getHistoryDetail({ payload, callback }, { call, put }) {
            const response = yield call(getRegularHistory, payload);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        historyData: response.data
                    }
                });
            }
            callback(response);
        },
        // 获取转正历史记录-个人管理
        *getPersonHistoryDetail({ payload, callback }, { call, put }) {
            const response = yield call(getRegularHistoryPerson, payload);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        historyData: response.data
                    }
                });
            }
            callback(response);
        }

    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        clear() {
            return {
                data: [],
                historyData: [],
                pagination: {
                    current: 1,
                    pageSize: 10,
                    total: 0
                }
            };
        }
    }
};
